/*
 * Copyright 2022 The Open Islands Authors. All Rights Reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.openislands.oi.console.command;

import org.openislands.oi.console.config.RequestWrapper;
import org.openislands.oi.console.config.annotation.Command;
import org.openislands.oi.console.config.annotation.CommandGroup;
import org.openislands.oi.console.config.annotation.CommandParam;
import org.openislands.oi.pojo.qo.NodeRouteQO;
import org.openislands.oi.pojo.qo.RouteUpdateQO;
import org.openislands.oi.pojo.vo.CommonResponse;
import org.openislands.oi.pojo.vo.NodeRouteVO;
import org.openislands.oi.util.ObjectUtils;
import org.openislands.oi.apiserver.http.RouteController;

import java.util.List;
import java.util.Map;

@SuppressWarnings("unused")
@CommandGroup("route")
public class RouteCommand {

    @Command(value = "add", desc = "add union node route")
    public RequestWrapper<RouteController, CommonResponse<Boolean>> create(
            @CommandParam String nodeId,
            String httpAccess,
            @CommandParam(required = false) String httpsAccess,
            @CommandParam(required = false) String grpcAccess,
            @CommandParam(required = false) String grpcsAccess,
            @CommandParam(value = "--tls", required = false, defaultValue = "false") Boolean tlsEnabled) {

        return new RequestWrapper<RouteController, CommonResponse<Boolean>>() {
            @Override
            public CommonResponse<Boolean> request(RouteController target, Map<String, Object> commandParams) {
                return target.addRoute(ObjectUtils.mapToObject(commandParams, NodeRouteQO.class));
            }

            @Override
            public String resultString(CommonResponse<Boolean> result) {
                return ObjectUtils.object2JsonIndent(result);
            }
        };
    }

    @Command(desc = "delete route by id")
    public RequestWrapper<RouteController, CommonResponse<Boolean>> delete(
            @CommandParam(desc = "route id") Long id) {
        return new RequestWrapper<RouteController, CommonResponse<Boolean>>() {
            @Override
            public CommonResponse<Boolean> request(RouteController target, Map<String, Object> commandParams) {
                return target.deleteRoute(Long.valueOf(commandParams.get("id").toString()));
            }

            @Override
            public String resultString(CommonResponse<Boolean> result) {
                return ObjectUtils.object2JsonIndent(result);
            }
        };
    }

    @Command(desc = "update union node route")
    public RequestWrapper<RouteController, CommonResponse<Boolean>> update(
            @CommandParam(desc = "route id") Long id,
            @CommandParam(required = false) String httpAccess,
            @CommandParam(required = false) String httpsAccess,
            @CommandParam(required = false) String grpcAccess,
            @CommandParam(required = false) String grpcsAccess,
            @CommandParam(value = "--tls", required = false, defaultValue = "false") Boolean tlsEnabled) {
        return new RequestWrapper<RouteController, CommonResponse<Boolean>>() {
            @Override
            public CommonResponse<Boolean> request(RouteController target, Map<String, Object> commandParams) {
                return target.updateRoute(ObjectUtils.mapToObject(commandParams, RouteUpdateQO.class));
            }

            @Override
            public String resultString(CommonResponse<Boolean> result) {
                return ObjectUtils.object2JsonIndent(result);
            }
        };
    }

    @Command(desc = "query node route by nodeId")
    public RequestWrapper<RouteController, CommonResponse<List<NodeRouteVO>>> query(
            @CommandParam(desc = "route home node id") String nodeId) {
        return new RequestWrapper<RouteController, CommonResponse<List<NodeRouteVO>>>() {
            @Override
            public CommonResponse<List<NodeRouteVO>> request(RouteController target, Map<String, Object> commandParams) {
                return target.queryByNodeId(commandParams.get("nodeId").toString());
            }

            @Override
            public String resultString(CommonResponse<List<NodeRouteVO>> result) {
                return ObjectUtils.object2JsonIndent(result);
            }
        };
    }
}
